package com.bharatonjava.college.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.bharatonjava.college.domain.Enumeration;

public class CommonDaoImpl implements CommonDao{

	private JdbcTemplate jdbcTemplate;
	
	private String enumsQuery;
	
	public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
	
	public void setEnumsQuery(String enumsQuery) {
		this.enumsQuery = enumsQuery;
	}
	
	@Override
	public Set<Enumeration> getEnumerationsByGroupId(String enumGroup) {

		Set<Enumeration> enums = new HashSet<Enumeration>();
		
		jdbcTemplate.query(this.enumsQuery,new RowMapper<Enumeration>() {

			@Override
			public Enumeration mapRow(ResultSet rs, int rowNum) throws SQLException {
				Enumeration e = new Enumeration();
				e.setEnumId(rs.getInt("ENUM_ID"));
				e.setEnumName(rs.getString("ENUM_NAME"));
				e.setEnumGroup(rs.getString("ENUM_GROUP"));
				return e;
			}
			
		});
		return enums;
	}

	
}
